package com.liuyang.ecommerce.notifier;

import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
import de.codecentric.boot.admin.server.notify.AbstractEventNotifier;
import lombok.extern.apachecommons.CommonsLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * 自定义报警
 */
@Slf4j
@Component
@SuppressWarnings("all")
public class MyNotifier extends AbstractEventNotifier {

    public MyNotifier(InstanceRepository instanceRepository) {
        super(instanceRepository);
    }

    /**
     * 实现对时间的通知
     * @param event 实例发生的哪一个事件？
     * @param instance 哪一个实例
     * @return
     */
    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        return Mono.fromRunnable(()-> {
            if(event instanceof InstanceStatusChangedEvent) {
                log.info("状态发生了改变:[{}],[{}],[{}]",instance.getRegistration().getName(),
                        event.getInstance(),((InstanceStatusChangedEvent) event).getStatusInfo().getStatus());
            } else {
                log.info("Instance info :[{}},[{}],[{}]",instance.getRegistration().getName(),event.getInstance(),event.getType());
            }
        });
    }
}
